/*
 * @(#)CalendarUtility.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
     * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */

package com.dcivision.calendar;

import java.sql.Connection;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.dcivision.calendar.bean.MtmCalendarRecordMeeting;
import com.dcivision.calendar.dao.CalendarRecordDAObject;
import com.dcivision.calendar.dao.MtmCalendarRecordMeetingDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.Utility;
import com.dcivision.user.bean.UserRecord;
import com.dcivision.user.dao.UserRecordDAObject;

/**
  CalendarUtility.java
  This class is the data access bean for table "CONTACT_COMPANY".
  @author      Jenny Li
  @company     DCIVision Limited
  @creation date   6/12/2003
  @version     $Revision: 1.6 $
 */

public class CalendarUtility {

  public static final String REVISION = "$Revision: 1.6 $";

  public static final String OBJECT_TYPE_STAFF = "S";
  public static final String TIME_FORMFAT_A = "hh:mm a";

  static Logger logger = Logger.getLogger("com.dcivision.calendar");

  /**
   *
   * @param sessionContainer
   * @param conn
   * @return
   */
  public synchronized Integer getStaffRecordIDByUserID(SessionContainer
      sessionContainer, Connection conn) {

    Integer staffID = new Integer( -1);

    try {
      UserRecordDAObject userDAO = new UserRecordDAObject(sessionContainer,
          conn);
      UserRecord user = new UserRecord();
      user = (UserRecord) userDAO.getObjectByID(sessionContainer.
                                                getUserRecordID());
      if (OBJECT_TYPE_STAFF.equals(user.getUserType())) {
        staffID = user.getUserLinkID();
      }
    }
    catch (Exception e) {
      logger.error(e, e);
    }
    finally {
    }

    return staffID;
  }

  /**
   *
   * @param ID
   * @param sessionCtx
   * @param conn
   * @return
   */
  public synchronized boolean checkInvitedToMeeting(Integer ID,
      SessionContainer sessionCtx, Connection conn) {

    List meetingList = null;
    String[] meetingUserIDs = null;

    try {
      MtmCalendarRecordMeetingDAObject calendarRecordMeetingDAO = new
          MtmCalendarRecordMeetingDAObject(sessionCtx, conn);
      meetingList = calendarRecordMeetingDAO.getListByCalendarID(ID);

      meetingUserIDs = new String[meetingList.size()];
      for (int i = 0; i < meetingList.size(); i++) {
        meetingUserIDs[i] = ( (MtmCalendarRecordMeeting) meetingList.get(i)).
            getInvitedID().toString();
      }
    }
    catch (Exception e) {
      logger.error(e, e);
    }
    finally {
    }

    CalendarUtility util = new CalendarUtility();

    return arrayContainsElement(meetingUserIDs,
                                util.getStaffRecordIDByUserID(sessionCtx, conn).
                                toString());
  }

  /**
   *
   * @param array
   * @param element
   * @return
   */
  public static boolean arrayContainsElement(String[] array, String element) {
    ArrayList list = (ArrayList) Utility.getList(array);

    return list.contains(element);
  }

  /**
   * Check overlap
   * @param sessionContainer SessionContainer
   * @param conn             Connection
   * @param userID           Interger user record ID
   * @param dateTime         Timestamp
   * @param endTime          Timestamp
   * @param opMode           String
   * @return
   * @throws ApplicationException
   */
  public static String checkOverlap(SessionContainer sessionContainer,
                                    Connection conn,
                                    Timestamp dateTime,
                                    Timestamp endTime,
                                    String opMode) throws ApplicationException {

    try {
      int recNum = 0; //check for edit mode, if overlap more then 1 impiles that there are rec overlap
      if ( (GlobalConstant.OP_MODE_INSERT.equals(opMode)) ||
          (GlobalConstant.OP_MODE_INSERT_NEW.equals(opMode))) {
        recNum = 0;
      }
      else {
        recNum = 1;
      }
      CalendarRecordDAObject calendarRecordDAO = new CalendarRecordDAObject(
          sessionContainer,
          conn);

      ArrayList list = (ArrayList) calendarRecordDAO.
          getListByDatePeriodAndEventTypeAndCalendarID(
          sessionContainer.getUserRecordID(), dateTime,
          endTime, null, null, null);
      if (list.size() > recNum) {
        return "true";
      }
      else {
        return "false";
      }
    }
    catch (ApplicationException appEx) {
      throw appEx;
    }
/*
    should not close connection which is passed from caller
    finally {
      try {
        conn.close();
      }
      catch (Exception ignore) {
      }
      finally {
        conn = null;
      }
    }
*/
  }

}